import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { CityData } from '../../city.data';
import { StorageService } from '../../service/storage.service';
import { Router } from '@angular/router';
declare var $: any;
import * as _ from 'underscore';

@Component({
  selector: 'app-certification',
  templateUrl: './certification.component.html',
  styleUrls: ['./certification.component.less']
})
export class CertificationComponent implements OnInit {

  public verification = false;
  public realname = '';
  public idcard = '';
  public _cityList = [];
  public areaList = [];
  public province = '';
  public city = '';
  public company = '';
  public tel = '';
  public wechat = '';
  public holdidcard = '';
  public workcard = '';
  public pic = '';
  public param = '';
  public imgUrl = [];


  constructor(
            private _http: HttpService,
            private _router: Router,
            private _storage: StorageService,
            private _httpRouter: HttpRouterService

  ) { }

  ngOnInit() {
    this._cityList = CityData;

    if (this._storage.getSessionObject('user').verification === 1) {
      this.verification = false;
    }else if (this._storage.getSessionObject('user').verification === 2) {
      this.verification = true;

    }

  }

  getArea($event){
    let city = _.find(this._cityList, function(item){
      return  item.value == $event.target.value;
    });
    this.areaList = city ? city.children : [];
  }

  uploadImage(id) {
    const formdata = new FormData();
    console.log($('#' + id)[0].files);
    formdata.append('myfile', $('#' + id)[0].files[0]);
    const that = this;
    $.ajax({
      type: 'POST',
      url: 'http://xdm.mengzhudai.com/api/upload/commissioner',
      data: formdata,
      processData: false,
      contentType: false,
      dataType: 'json',
      success: function(data) {
        $('#' + id).parent().prev().html('1/1');
        $('#' + id).parent().append(`<img style="position:absolute;top:0;left:0;display:block;width:70px;height:70px;"
        src="http://xdm.mengzhudai.com/${data.data.substring(12, data.data.length - 2)}"/>`);
        that.imgUrl.push(`http://xdm.mengzhudai.com/${data.data.substring(12, data.data.length - 2)}`);
        that.holdidcard = that.imgUrl[0];
        that.workcard = that.imgUrl[1];
        that.pic = that.imgUrl[2];
      },
      error: function(jqXHR){
        alert('发生错误：' + jqXHR.status);
      }
    });
  }

  certification($event) {
    $event.preventDefault();
    if (!this.realname) {
      alert('请输入真实姓名');
      return;
    }
    if (!this.idcard) {
      alert('请输入身份证号');
      return;
    }
    if (!this.province) {
      alert('请输入所在地区');
      return;
    }
    if (!this.city) {
      alert('请输入所在城市');
      return;
    }
    if (!this.company) {
      alert('请输入公司全称');
      return;
    }
    if (!this.wechat) {
      alert('请输入微信号');
      return;
    }
    if (!this.holdidcard) {
      alert('请上传手持身份证照片');
      return;
    }
    if (!this.workcard) {
      alert('请上传名片或工牌照片');
      return;
    }
    if (!this.pic) {
      alert('请上传本人与公司标志合影');
      return;
    }
    this.param = 'token=' + this._storage.getSession('token')
               + '&realname=' + this.realname
               + '&idcard=' + this.idcard
               + '&province=' + this.province
               + '&city=' + this.city
               + '&company=' + this.company
               + '&tel=' + this.tel
               + '&wechat=' + this.wechat
               + '&holdidcard=' + this.holdidcard
               + '&workcard=' + this.workcard
               + '&pic=' + this.pic;
    this._http.post(this._httpRouter.certification, this.param).then( res => {
      if (res.code === 1) {
        this._storage.setSessionObject('user', res.data);
        alert("提交成功！");
        this._router.navigate(['attache/personal']);
        return;
      }
      alert("失败，请重新提交！");
    });
  }

}
